<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  name: 'ScatterDiagram',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    // headTitle: {
    //   type: String,
    //   default: 'chart'
    // },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    // autoResize: {
    //   type: Boolean,
    //   default: true
    // },
    chartData: {
      type: Array,
      required: true
    }
    // yMax: {
    //   type: Number,
    //   required: true
    // },
    // interval: {
    //   type: Number,
    //   required: true
    // }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.resizeHandler)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeHandler)
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      this.chart.setOption({
        color: ['#768DD1'],
        xAxis: {
          name: '温度(℃)',
          splitLine: {
            show: true
          },
          min: 19.5,
          max: 25.5,
          interval: 0.5,
          axisLabel: {
            formatter: '{value}°C'
          }
        },
        yAxis: {
          name: '湿度(%)',
          splitLine: {
            show: true
          },
          min: 25,
          max: 70,
          interval: 5,
          axisLabel: {
            formatter: '{value}%'
          }
        },
        tooltip: {
          showDelay: 1,
          //formatter: '观测值<br />{c}'
          formatter: function (params) {
            return (
              '观测值：' +
              params.value[0] +
              '℃ ，' +
              params.value[1] +
              '% '
            );
          }
        },
        series: [
          {
            symbolSize: 5,
            data: chartData,
            type: 'scatter',
            markArea: {
              itemStyle: {
                color: 'rgba(118, 141, 209, 0.4)'
              },
              data: [
                [
                  {
                    name: '适宜范围',
                    xAxis: '20',
                    yAxis: '40'
                  },
                  {
                    xAxis: '25',
                    yAxis: '60'
                  }
                ]
              ]
            }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
</style>
